﻿/*

/-----------------------------------------------\
|          CODE by Kirati Isarapungpun          |
|       For North - Chiang Mai University       |
|       Software Engineering Project 2013       |
|                                               |
|  |-|>===================================<|-|  |
|                                               |
|       Course planning for Bachelor of         |
| Science degree in software engineering 2012   | 
|      Faculty of Science and Technology,       |
|        North - Chiang Mai University          |
|                                               |
\-----------------------------------------------/

/														\
	sPlan	:	 งานทุกอย่างที่เกี่ยวกับการวางแผนการลงทะเบียนของนักศึกษา
	เพิ่ม ลบ คำนวณเกรด คำนวณGPA
\														/


*/

var loopYear = 2012,startYear = 2012,maxYear = 0;
$(function()
{
	getyear();
	$('#addYear').button();
	getsplandata();
});

function getyear()
{
	$.get('class/_reqyear.php',function(data){
		window.startYear = parseInt(data);
		window.loopYear = window.startYear;
	});
}

function getsplandata()
{
	window.loopYear = window.startYear;
	$('#plantable').html('');
	var loopTerm = 1;
	$.get('class/_reqplanmax.php',function(data){
		window.maxYaer = parseInt(data);
		while(loopYear < maxYaer+1){
			$('#plantable').html($('#plantable').html()+'<tr><th colspan="3">Year '+loopYear+'</th></tr><tr><th id="th1">Semster 1</th><th id="th2">Semster 2</th></tr><tr><td><table id="'+loopYear+'term1"></table></td><td><table id="'+loopYear+'term2"></table></td></tr><tr><th colspan="2" id="th3">Semster 3</th></tr><tr><td colspan="2"><table id="'+loopYear+'term3"></table></td></tr>');
			loopTerm = 1;
			while(loopTerm < 4){
				$('#'+loopYear+'term'+loopTerm).html('<tr><th><input type="hidden" id="count'+loopYear+'_'+loopTerm+'" value="0">Subject Number</th><th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Subject&nbsp;Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th><th>Credit</th><th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pre&nbsp;Subject&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th><th>Grade<br>&nbsp;&nbsp;(Real)&nbsp;&nbsp;</th><th>Grade<br>(Planing)</th><th>Delete</th></tr>');
				loopTerm++;
			}
			loopYear++;
		}
	});
	
	$.getJSON('class/_reqplan.php',function(data)
	{
		$.each(data,function(key,val){
			$('#count'+val['year']+'_'+val['term']).val(parseInt($('#count'+val['year']+'_'+val['term']).val())+1);
			$('#'+val['year']+'term'+val['term']).html($('#'+val['year']+'term'+val['term']).html()+'<tr id="data'+val['id']+'"><input type="hidden" id="index'+val['year']+'_'+val['term']+'_'+$('#count'+val['year']+'_'+val['term']).val()+'" value="'+val['id']+'"><td id="snumber'+val['id']+'">'+val['sNumber']+'</td><td id="sName'+val['id']+'"><input type="hidden" id="subject'+val['id']+'" value="'+ val['sid']+'">'+ val['sName']+'</td><td id="sCredit'+val['id']+'">'+val['sCredit']+'</td><td id="sPre'+val['id']+'"><input type="hidden" id="preSubject'+val['id']+'" value="'+ val['sPreId']+'">'+val['sPreName']+'</td><td id="grade'+val['id']+'"><div title="Click to change" onClick="gradeChanger('+val['id']+',1,\'#grade'+val['id']+'\');">'+val['grade']+'</div></td><td id="gradeP'+val['id']+'"><div title="Click to change" onClick="gradeChanger('+val['id']+',2,\'#gradeP'+val['id']+'\');">'+val['gradeP']+'<div></td><td><li class="ui-state-default ui-corner-all" id="delbutton'+val['id']+'" title="Delete" onClick="plandelcon('+val['id']+');"><span class="ui-icon ui-icon-trash"></span></li></td></tr>');
			//$('#snumber'+val['id']).button().css({'cursor': 'default','width': '30'});
			//$('#sName'+val['id']).button().css({'cursor': 'default','width': '200'});
			//$('#sCredit'+val['id']).button().css({'cursor': 'default','width': '30'});
			getplan('#sName'+val['id'],val['sid'],val['year'],val['term'],0);
			$('#grade'+val['id']).button().css({'cursor': 'pointer','width': '200'});
			$('#gradeP'+val['id']).button().css({'cursor': 'pointer','width': '200'});
			$('#delbutton'+val['id']).button().css({'cursor': 'pointer','height':'35','width': '50'});
		});
		
		window.loopYear = window.startYear;
		while(loopYear < window.maxYaer+1){
			loopTerm = 1;
			while(loopTerm < 4){
				$('#'+loopYear+'term'+loopTerm).html($('#'+loopYear+'term'+loopTerm).html()+'<tr><td id="'+loopYear+'addterm'+loopTerm+'" colspan="7" onClick="planaddbutton('+loopYear+','+loopTerm+');">ADD</td></tr>');
				$('#'+loopYear+'addterm'+loopTerm).button().css({'width': '500'});
				$('#'+loopYear+'term'+loopTerm).html($('#'+loopYear+'term'+loopTerm).html()+'<tr><th></th><th>Credit :</th><th id="'+loopYear+'totalCredit'+loopTerm+'"></th><th>Grade :</th><th id="'+loopYear+'totalGrade'+loopTerm+'"></th><th id="'+loopYear+'totalGradePlan'+loopTerm+'"></th><th></th></tr>');
				loopTerm++;
			}
			$('#plantable').html($('#plantable').html()+'<tr><th colspan="3">Year'+loopYear+' : Credit : <strong id="yearCredit'+loopYear+'"></strong> Grade : (Real) <strong id="yearGPA'+loopYear+'"></strong>  / (Planing) <strong id="yearGPAP'+loopYear+'"> / </strong>  </th> ');
			loopYear++;
			
		}
		calculategrade();
	});
	
	
}

function addYear()
{
	var loopYear = window.loopYear;
	$('#plantable').html($('#plantable').html()+'<tr><th colspan="3">Year '+loopYear+'</th></tr><tr><th id="th1">Term1</th><th  id="th2">Term2</th></tr><tr><td><table id="'+loopYear+'term1"></table></td><td><table id="'+loopYear+'term2"></table></td></tr><tr><th colspan="2"  id="th3">Term3</th></tr><tr><td colspan="2"><table id="'+loopYear+'term3"></table></td></tr>');
	var loopTerm = 1;
	while(loopTerm < 4){
		$('#'+loopYear+'term'+loopTerm).html('<tr><th><input type="hidden" id="count'+loopYear+'_'+loopTerm+'" value="0">Subject Number</th><th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Subject&nbsp;Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th><th>Credit</th><th>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Pre&nbsp;Subject&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th><th>Grade(Real)</th><th>Grade(planing)</th><th>Delete</th></tr>');
		$('#'+loopYear+'term'+loopTerm).html($('#'+loopYear+'term'+loopTerm).html()+'<tr><td id="'+loopYear+'addterm'+loopTerm+'" colspan="7" onClick="planaddbutton('+loopYear+','+loopTerm+');">ADD</td></tr>');
		$('#'+loopYear+'addterm'+loopTerm).button().css({'width': '500'});
		loopTerm++;
	}
	$('#addYear').hide();
}

function planaddbutton(year,term)
{
	$.get('ui/planselectdialog.php',function(data)
	{
	$('#plandialog').html(data);
	pdepselect('#pselectdepadd');
	$('#pselectdepadd').change(function () {
		$('#pselectdepadd option:selected').each(function () {
			pgopselect('#pselectgopadd',$('#pselectdepadd').val());
		});
	}).trigger('change');
	$('#pselectgopadd').change(function () {
		$('#pselectgopadd option:selected').each(function () {
			psubselect('#pselectsubadd',$('#pselectgopadd').val());
		});
	}).trigger('change');
	$('#pselectsubadd').change(function () {
		$('#pselectsubadd option:selected').each(function () {
			$.getJSON('class/_reqonesub.php',{ sid : $('#pselectsubadd').val()},function(data)
			{
				$.each(data,function(key,val){
					$('#pdetail').html('Credit :'+val['credit']+'<br> Pre Subject : '+val['sName']+'<br> Open : ');
					getplan('#pdetail',$('#pselectsubadd').val(),year,term,1);
				});
			});
		});
	}).trigger('change');
	$('#plandialog').dialog({ 'title':'Add','width':'500',buttons: [{text: 'Refresh',click: function()
		{ 
			$( this ).dialog( 'close' ); 
			planaddbutton(year,term);
		}},{ text: 'ADD', click: function() 
		{ 
			planadd(year,term);
			$( this ).dialog( 'close' ); 
		}},{ text: 'Cancel', click: function() { $( this ).dialog( 'close' ); } }]});
	});
}

function getplan(where,sid,year,term,mode)
{
	if(mode == 1)
	{
		$.get('class/_reqoneplan.php',{'sid':sid,'year':year,'term':term},function(data){
			if(data == 1) {
				$(where).html($(where).html()+' Open');
			} else if(data == 0) {
				$(where).html($(where).html()+' Not Open');
			}
		});
	} else {
		$.get('class/_reqoneplan.php',{'sid':sid,'year':year,'term':term},function(data){
			if(data == 1) {
				$(where).css({'color': 'black'});
			} else if(data == 0) {
				$(where).css({'color': 'gray'});
			}
		});
	}
}

function pdepselect(where)
{
		$(where).html('<option value=""></option>');
		$.getJSON('class/_reqdep.php',function(data)
		{
			$.each(data,function(key,val){
			$(where).html($(where).html()+'<option value="'+val['id']+'">'+val['number']+' - '+val['name']+'</option>');
			});
		});
}

function pgopselect(where,id)
{
		$(where).html('<option value=""></option>');
		$.getJSON('class/_reqgop.php',function(data)
		{
			$.each(data,function(key,val){
				if(val['did'] == id)
				{
					$(where).html($(where).html()+'<option value="'+val['id']+'">'+val['number']+' - '+val['name']+'</option>');
				}
			});
		})
}

function psubselect(where,id)
{
		$(where).html('<option value=""></option>');
		$.getJSON('class/_reqsub.php',function(data)
		{
			$.each(data,function(key,val){
			if(val['gid'] == id)
			{
				$(where).html($(where).html()+'<option value="'+val['id']+'">'+val['number']+' - '+val['name']+'</option>');
			}
			});
		});
}

function gradeChanger(id,turn,where)
{
	$(where).html('<select id="gradechanger'+id+'_'+turn+'"></select>');
	$('#gradechanger'+id+'_'+turn).html('<option value=""></option>');
	$('#gradechanger'+id+'_'+turn).html($('#gradechanger'+id+'_'+turn).html()+'<option value="F">F</option>');
	$('#gradechanger'+id+'_'+turn).html($('#gradechanger'+id+'_'+turn).html()+'<option value="D">D</option>');
	$('#gradechanger'+id+'_'+turn).html($('#gradechanger'+id+'_'+turn).html()+'<option value="D+">D+</option>');
	$('#gradechanger'+id+'_'+turn).html($('#gradechanger'+id+'_'+turn).html()+'<option value="C">C</option>');
	$('#gradechanger'+id+'_'+turn).html($('#gradechanger'+id+'_'+turn).html()+'<option value="C+">C+</option>');
	$('#gradechanger'+id+'_'+turn).html($('#gradechanger'+id+'_'+turn).html()+'<option value="B">B</option>');
	$('#gradechanger'+id+'_'+turn).html($('#gradechanger'+id+'_'+turn).html()+'<option value="B+">B+</option>');
	$('#gradechanger'+id+'_'+turn).html($('#gradechanger'+id+'_'+turn).html()+'<option value="A">A</option>');
	$('#gradechanger'+id+'_'+turn).html($('#gradechanger'+id+'_'+turn).html()+'<option value="CX" title="เทียบโอน">CX</option>');
	$('#gradechanger'+id+'_'+turn).html($('#gradechanger'+id+'_'+turn).html()+'<option value="I" title="วัดผลไม่สมบูณ์">I</option>');
	
	
		$('#gradechanger'+id+'_'+turn).change(function () {
			$('#gradechanger'+id+'_'+turn+' option:selected').each(function () {
				if($('#gradechanger'+id+'_'+turn).val() != '')
				{
					$.get('class/_reqplangrade.php',{'id':id,'turn':turn,'grade':$('#gradechanger'+id+'_'+turn).val()},function(data)
					{
						$(where).html('<div title="Click to change" onClick="gradeChanger('+id+','+turn+',\''+where+'\');">'+$('#gradechanger'+id+'_'+turn).val()+'</div>');
						calculategrade()
					});
				}
			});
		}).trigger('change');
}

function planadd(year,term)
{
	if($('#pselectdepadd').val() == '')
	{
		$('#dialog').html('Plase enter Deparment');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#pselectgopadd').val() == '')
	{
		$('#dialog').html('Plase enter Subject Group');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else if($('#pselectsubadd').val() == '')
	{
		$('#dialog').html('Plase enter Subject');
		$('#dialog').dialog({'title':'Error'});
		$('#dialog').dialog('option','resizable',false);
		$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
	}else
	{
		$.get('class/_reqplaninsert.php',{'sid':$('#pselectsubadd').val(),'year':year,'term':term},function(data)
		{
			$('#dialog').html('Insert Success');
			$('#dialog').dialog({'title':'Success'});
			$('#dialog').dialog('option','resizable',false);
			$('#dialog').dialog({ buttons: [{text: 'OK',click: function(){$( this ).dialog('close');}}]});
			getsplandata();
		});
	}
}

function plandelcon(id)
{
	$('#delcon').html('You want to Delete this?');
		$('#delcon').dialog({ buttons: [{text: 'Delete',click: function()
		{ 
			plandeldata(id);
			$( this ).dialog('close'); 
		}},{ text: 'Cancel', click: function() { $( this ).dialog( 'close' ); } }]});
		$('#delcon').dialog({'title':'Confirm Delete'});
		$('#delcon').dialog('option','resizable',false);
}

function plandeldata(id)
{
	$.get('class/_reqplandel.php',{'id':id},function(data)
	{
		$('#dialog').html('Delete Success');
		$('#dialog').dialog({'title':'Success'});
		$('#dialog').dialog('option','resizable',false);
		getsplandata();
	});
}

function calculategrade()
{
	var loopYear = 2012,loopTerm = 0,loopCount = 0,count = 0,grade = 0,gradeP = 0,credit = 0,creditPlan = 0,GPA = 0,GPAP = 0,totalCredit = 0,totalCreditPlan = 0,yearCredit = 0,yearCreditPlan = 0,yearGrade = 0,yearGradePlan = 0,yearGPA = 0,yearGPAP = 0,totalGrade = 0,totalGradePlan = 0,totalGPA = 0,totalGPAP = 0,precheck = 0,aftercheck = 0;
	while(loopYear < window.maxYaer+1){
		loopTerm = 1;
		yearCredit = 0;yearCreditPlan = 0;yearGrade = 0;yearGradePlan = 0;yearGPA = 0;yearGPAP = 0;
		while(loopTerm < 4){
			count = parseInt($('#count'+loopYear+'_'+loopTerm).val());
			loopCount = 1;grade = 0;gradeP = 0;credit = 0;creditPlan = 0;GPA = 0;GPAP = 0;
			while(loopCount < count+1){
				precheck = preReqSubjectCheck($('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val(),loopYear,loopTerm);
				aftercheck = afterSubjectCheck($('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val(),loopYear,loopTerm);
				if(precheck == 1 && aftercheck == 1)
				{
					if($('#grade'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text() != 'I' && $('#grade'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text() != 'CX' && $('#grade'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text() != '0')
					{
						credit += parseInt($('#sCredit'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text());
						grade += (gradeConverter($('#grade'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text())*parseFloat($('#sCredit'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text()));
						GPA = grade/credit;
						if ($('#grade'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text() == 'F') {
							$('#grade'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).css({'color': 'red'});
						} else {
							$('#grade'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).css({'color': 'black'});
						}
					} else if($('#grade'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text() == 'CX') {
						$('#grade'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).css({'color': 'white'});
					} else {
						$('#grade'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).css({'color': 'red'});
					}
					if($('#gradeP'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text() != 'I' && $('#gradeP'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text() != 'CX' && $('#gradeP'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text() != '0')
					{
						creditPlan += parseInt($('#sCredit'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text());
						gradeP += (gradeConverter($('#gradeP'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text())*parseFloat($('#sCredit'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text()));
						GPAP = gradeP/credit;
						if ($('#gradeP'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text() == 'F'){
							$('#gradeP'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).css({'color': 'red'});
						} else {
							$('#gradeP'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).css({'color': 'black'});
						}
					} else if($('#gradeP'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text() == 'CX') {
						$('#gradeP'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).css({'color': 'white'});
					} else {
						$('#gradeP'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).css({'color': 'red'});
					}
				} else {
					$('#grade'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).css({'color': 'white'});
					$('#gradeP'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).css({'color': 'white'});
				}
				loopCount++;
			}
			if(credit != 0) {
				$('#'+loopYear+'totalCredit'+loopTerm).html(credit);
				if(loopTerm < 3){
					if(credit > 21) {
						$('#'+loopYear+'totalCredit'+loopTerm).css({'color': 'red'});
						$('#'+loopYear+'totalCredit'+loopTerm).html('<strong title="หนว่ยกิตเกิน">'+$('#'+loopYear+'totalCredit'+loopTerm).html()+'</strong>');
					}
				} else {
					if(credit > 9) {
						$('#'+loopYear+'totalCredit'+loopTerm).css({'color': 'red'});
						$('#'+loopYear+'totalCredit'+loopTerm).html('<strong title="หนว่ยกิตเกิน">'+$('#'+loopYear+'totalCredit'+loopTerm).html()+'</strong>');
					}
				}
				totalCredit += credit;
				yearCredit += credit;
				totalCreditPlan += creditPlan;
				yearCreditPlan += creditPlan;
				$('#'+loopYear+'totalGrade'+loopTerm).html(GPA.toFixed(2));
				totalGrade += grade;
				yearGrade += grade;
				$('#'+loopYear+'totalGradePlan'+loopTerm).html(GPAP.toFixed(2));
				totalGradePlan += gradeP;
				yearGradePlan += gradeP;
			}else {
				$('#'+loopYear+'totalCredit'+loopTerm).html('');
				$('#'+loopYear+'totalGrade'+loopTerm).html('');
				$('#'+loopYear+'totalGradePlan'+loopTerm).html('');
			}
			loopTerm++;
		}
		yearGPA = yearGrade/yearCredit;
		yearGPAP = yearGradePlan/yearCreditPlan;
		$('#yearCredit'+loopYear).html(yearCredit);
		$('#yearGPA'+loopYear).html(yearGPA.toFixed(2));
		$('#yearGPAP'+loopYear).html(yearGPAP.toFixed(2));
		loopYear++;
	}
	totalGPA = totalGrade/totalCredit;
	totalGPAP = totalGradePlan/totalCreditPlan;
	$('#totalCredit').html(totalCredit);
	$('#totalGrade').html(" (Real) <br>"+totalGPA.toFixed(2));
	$('#totalGradePlan').html(" (Planing) <br>"+totalGPAP.toFixed(2));
}

function preReqSubjectCheck(id,year,term)
{
	var loopYear = year,limtYear = 2012,limtTerm = 0,loopTerm = 0,loopCount = 0,grade = 0;
	if($('#preSubject'+id).val() != ''){
		while(loopYear >= limtYear){
			if(loopYear == year){
				loopTerm = term-1;
			} else {
				loopTerm = 3;
			}
			if(loopTerm <= 0){
				if(loopYear == limtYear) {
					$('#sPre'+id).css({'color': 'red'});
					return 0;
				}
			} else {
				while(loopTerm > 0){
					loopCount = parseInt($('#count'+loopYear+'_'+loopTerm).val());
					while(loopCount > 0){
						if($('#subject'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).val() == $('#preSubject'+id).val()){
							grade = gradeConverter($('#grade'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).text())
							if(grade > 0){
								$('#sPre'+id).css({'color': 'green'});
								return 1;
							} else {
								$('#sPre'+id).css({'color': 'red'});
								return 0;
							}
						} else {
							$('#sPre'+id).css({'color': 'red'});
						}
						loopCount--;
					}
					loopTerm--;
				}
			}
			loopYear--;
		}
	} else {
		return 1;
	}
}

function afterSubjectCheck(id,year,term)
{
	var loopYear = year,loopTerm = 0,loopCount = 0,count = 0,check = 1;
	while(loopYear < window.maxYaer+1){
		if(loopYear == year){
			loopTerm = term+1;
		} else {
			loopTerm = 1;
		}
		while(loopTerm < 4){
			count = parseInt($('#count'+loopYear+'_'+loopTerm).val());
			loopCount = 1;
			while(loopCount < count+1){
			//alert('at'+id+' > '+$('#subject'+id).val()+'='+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()+' > '+$('#subject'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).val());
				if($('#subject'+$('#index'+loopYear+'_'+loopTerm+'_'+loopCount).val()).val() == $('#subject'+id).val()){
					check = 0;
				}
				
			loopCount++;	
			}
			
		loopTerm++;		
		}
			
	loopYear++;		
	}
	return check;		
}

	
